package com.silver.weibo.utils;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//创建一个类型转化器类
public class StringToDateConverter implements Converter<String, Date> {
    private static final List<String> formarts = new ArrayList<>(12);

    static {
        formarts.add("yyyy-MM");
        formarts.add("yyyy-MM-dd");
        formarts.add("yyyy-MM-dd hh:mm");
        formarts.add("yyyy-MM-dd hh:mm:ss");
        formarts.add("yyyy/MM");
        formarts.add("yyyy/MM/dd");
        formarts.add("yyyy/MM/dd hh:mm");
        formarts.add("yyyy/MM/dd hh:mm:ss");
        formarts.add("yyyy.MM");
        formarts.add("yyyy.MM.dd");
        formarts.add("yyyy.MM.dd hh:mm");
        formarts.add("yyyy.MM.dd hh:mm:ss");
    }

    @Override
    public Date convert(String source) {
        String value = source.trim();
        if ("".equals(value)) {
            return null;
        }
        if (source.contains("-")){
            if (source.matches("^\\d{4}-\\d{1,2}$")) {
                return parseDate(source, formarts.get(0));
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
                return parseDate(source, formarts.get(1));
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(2));
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(3));
            } else {
                throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
            }
        } else if (source.contains("/")){
            if (source.matches("^\\d{4}/\\d{1,2}$")) {
                return parseDate(source, formarts.get(4));
            } else if (source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}$")) {
                return parseDate(source, formarts.get(5));
            } else if (source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(6));
            } else if (source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(7));
            } else {
                throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
            }
        } else {
            if (source.matches("^\\d{4}.\\d{1,2}$")) {
                return parseDate(source, formarts.get(8));
            } else if (source.matches("^\\d{4}.\\d{1,2}.\\d{1,2}$")) {
                return parseDate(source, formarts.get(9));
            } else if (source.matches("^\\d{4}.\\d{1,2}.\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(10));
            } else if (source.matches("^\\d{4}.\\d{1,2}.\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(11));
            } else {
                throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
            }
        }
    }

    /**
     * 格式化日期
     *
     * @param dateString String 字符型日期
     * @param format  String 格式
     * @return Date 日期
     */
    public Date parseDate(String dateString, String format) {
        Date date = null;
        try {
            date = new SimpleDateFormat(format).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}

